#include <QMutexLocker>
#include <QSettings>
#include <QTextCodec>
#include <QTextStream>
#include <QFile>
#include <QDir>
#include <QApplication>
#include <QJsonDocument>
#include <QJsonParseError>
#include <QJsonObject>
#include <QJsonArray>

#include "plugincustomplottest.h"
#include "../../DevBase/dllQLsThemesManager/qlsthemesmanager.h"
#include "../../AppFramework/dllQLsPluginsManagerAppfwk/qlspluginsmanagerappfwk.h"

static const QString g_pluginSelf = "pluginCustomPlotTest";
static const QString g_pluginsDir = "plugins/QCoreSystem";

/* static */
QMutex PluginCustomPlotTest::g_centerMutex;
QlsCustomPlotTest *PluginCustomPlotTest::g_centerWidget = nullptr;

PluginCustomPlotTest::PluginCustomPlotTest(QWidget *parent) :
	QWidget(parent),
	m_interfaceInfo(nullptr)
{
	/* 读取json文件格式内的插件数据 */
	ReadPluginDataFromJsonFile();
}

PluginCustomPlotTest::~PluginCustomPlotTest()
{
	disconnect(m_changeThemeConn);
	disconnect(m_changeMinimumSizeConn);
	if(m_interfaceInfo)
	{
		delete m_interfaceInfo;
		m_interfaceInfo = nullptr;
	}
	Destroy();
}

/* 获取插件信息 */
InterfaceInfo *PluginCustomPlotTest::GetInterfaceInfo()
{
	return m_interfaceInfo;
}

/* 返回一个Widget设置到centerwidget中进行显示 */
QWidget *PluginCustomPlotTest::GetPluginWidget()
{
	if(!g_centerWidget)
	{
		QMutexLocker locker(&g_centerMutex);
		if(!g_centerWidget)
			g_centerWidget = new QlsCustomPlotTest;
	}
	return g_centerWidget;
}

/* 销毁插件 */
void PluginCustomPlotTest::Destroy()
{
	if(g_centerWidget)
	{
		QMutexLocker locker(&g_centerMutex);
		if(g_centerWidget)
		{
			delete g_centerWidget;
			g_centerWidget = nullptr;
		}
	}
}

/* 绑定 切换主题 信号槽 */
void PluginCustomPlotTest::connetChangeTheme(void *sigClass)
{
	QLsThemesManager *themesManager = static_cast<QLsThemesManager *>(sigClass);
	m_changeThemeConn = connect(
				themesManager, &QLsThemesManager::sigChangeTheme,
				this, &PluginCustomPlotTest::slotChangeTheme
				);
}

/* 绑定 最小尺寸变化 的信号槽 */
void PluginCustomPlotTest::connectChangeMinimumSize(void *slotClass)
{
	QLsPluginsManagerAppfwk *pluginsManagerAppfwk = static_cast<QLsPluginsManagerAppfwk *>(slotClass);
	m_changeMinimumSizeConn = connect(
				g_centerWidget, &QlsCustomPlotTest::sigChangeMinimumSize,
				pluginsManagerAppfwk, &QLsPluginsManagerAppfwk::slotPluginsChangeMinimumSize
				);
}

/* 读取json文件格式内的插件数据 */
void PluginCustomPlotTest::ReadPluginDataFromJsonFile()
{
	/* 读取文件数据 */
	QFile loadFile(QString(":/%1.json").arg(g_pluginSelf));
	if(!loadFile.open(QIODevice::ReadOnly)) return;
	QByteArray allData = loadFile.readAll();
	loadFile.close();

	/* 判断json格式 */
	QJsonParseError json_error;
	QJsonDocument jsonDoc(QJsonDocument::fromJson(allData, &json_error));
	if(json_error.error != QJsonParseError::NoError) return;

	/* 获取数据并存入类中 */
	QJsonObject rootObj = jsonDoc.object();
	m_interfaceInfo = new InterfaceInfo(rootObj);
}

/* 切换主题的槽函数 */
void PluginCustomPlotTest::slotChangeTheme(const QString &themeName)
{
	/* 判断当前主题是否存在 或 有无变化 */
	if(m_curTheme == themeName) return;

	/* 判断主题文件夹和判断当前主题文件是否存在 "themes/plugins/QCoreSystem/QLsPluginDataAccess/themeName.qss" */
	QDir themesDir = QDir(qApp->applicationDirPath());
	QString themeFilename = QString("%1.qss").arg(themeName);
	if(!themesDir.cd(QString("themes/%1/%2").arg(g_pluginsDir).arg(g_pluginSelf)) || !themesDir.entryList().contains(themeFilename))
	{	/** TODO:如果没有，加载默认主题(暂无，直接加载插件管理器的主题) */
		/* 打开主题文件 */
		themesDir = QDir(qApp->applicationDirPath());
		if(themesDir.cd(QString("themes/%1").arg(g_pluginsDir)) && themesDir.entryList().contains(themeFilename))
		{
			QFile file(themesDir.absoluteFilePath(themeFilename));
			if(!file.open(QIODevice::ReadOnly)) return;

			/* 读取主题配置内容 */
			QTextStream in(&file);
			QString str = in.readAll();

			/* 加载主题：先清空上次样式表，否则会崩溃，qt的bug */
			g_centerWidget->setStyleSheet("");
			g_centerWidget->setStyleSheet(str);
		}

		m_curTheme = themeName;
		/* 更新配置文件 */
		QSettings settings(QString("ini/themes/%1.ini").arg(g_pluginsDir), QSettings::IniFormat);
		settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
		settings.setValue(QString("%1/theme").arg(g_pluginSelf), themeName);
		return;
	}

	/* 打开主题文件 */
	QFile file(themesDir.absoluteFilePath(themeFilename));
	if(!file.open(QIODevice::ReadOnly)) return;

	/* 读取主题配置内容 */
	QTextStream in(&file);
	QString str = in.readAll();

	/* 加载主题：先清空上次样式表，否则会崩溃，qt的bug */
	g_centerWidget->setStyleSheet("");
	g_centerWidget->setStyleSheet(str);
	m_curTheme = themeName;

	/* 更新配置文件 */
	QSettings settings(QString("ini/themes/%1.ini").arg(g_pluginsDir), QSettings::IniFormat);
	settings.setIniCodec(QTextCodec::codecForName("UTF-8"));
	settings.setValue(QString("%1/theme").arg(g_pluginSelf), themeName);
}
